/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:15:44
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-07-12 09:12:18
*/

import UserSubscribe from '../user-subscribe/user-subscribe.vue'

import Pages from '../../component/pages/pages.vue'
import { showMsg, getCastTalkWhiteList, updateCastTalkUser, queryCastTalkUserItems } from '../../vuex/action'

export default {
  name: 'cast-talk-user-list',
  components: {
    'vue-pages': Pages,
    'vue-user-subscribe': UserSubscribe
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      search: {
        mobile: '',
        name: '',
        company: ''
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.castTalkNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetlist: getCastTalkWhiteList,
      xQueryItems: queryCastTalkUserItems,
      xDelete: updateCastTalkUser
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      let params = {
        pageIndex: start,
        pageSize: limit
      }
      let {mobile, name, company} = this.search
      if (mobile) {
        params.mobile = mobile
      }
      if (name) {
        params.name = name
      }
      if (company) {
        params.company = company
      }
      this.xGetlist(params)
        .then((res) => {
          this.list = res.data.userItemList.map((item) => {
            item.circles = []
            return item
          })
          this.pages.totalPages = Math.ceil(res.data.total / this.pages.limit)

          this.queryUserItems()
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    queryUserItems() {
      for (let k in this.list) {
        let it = this.list[k]
        if (it.itemCount && it.itemCount > 0) {
          this.xQueryItems({
            userid: it.id
          })
            .then((res) => {
              this.list[k].circles = res.data.list
            })
            .catch((res) => {
              this.xShowMsg({
                type: 'danger',
                content: res.msg
              })
            })
        }
      }
    },
    searchUser() {
      this.getList({
        start: 0
      })
    },
    updateUser(user) {
      var ref = this.$root.$refs.userConfig
      ref.actionType = 'UPDATE'
      ref.form = {
        id: user.id,
        name: user.name,
        mobile: user.mobile,
        company: user.company,
        department: user.department,
        jobTitle: user.jobTitle,
        email: user.email,
        remark: user.remark
      }
      ref.showModal = true
    },
    subscribe(userId) {
      var ref = this.$refs.subscribe
      ref.form.userId = userId
      ref.form.type = -1
      ref.form.value = ''
      ref.showModal = true
    },
    deleteUser(userId) {
      this.xDelete({
        id: userId,
        status: 0
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
